//package ch.ethz.fcl.user.interaction;
//
//import ch.ethz.fcl.draw.Bounds;
//import ch.ethz.fcl.draw.Circle;
//import ch.ethz.fcl.draw.Line;
//import ch.ethz.fcl.udp.Protocal;
//import ch.ethz.fcl.udp.UDPClient;
//import android.graphics.PointF;
//import android.util.FloatMath;
//import android.util.Log;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.View.OnTouchListener;
//
//public class MultiTouchHandler implements OnTouchListener {
//	private final String TAG = "MultiTouchHandler";
//	private UDPClient client;
//	private Circle circle;
//	private Line line;
//
//	private PointF start = new PointF();
//	private PointF mid = new PointF();
//	private PointF old = new PointF();
//	private PointF pivot = new PointF();
//
//	private int mode;
//
//	private float oldDist = 0;
//	public static boolean cylinder = true;;
//
//	public MultiTouchHandler(Circle circle, Line line) {
//		this.client = new UDPClient();
//		client.run();
//
//		this.circle = circle;
//		this.line = line;
//	}
//
//	@Override
//	public boolean onTouch(View v, MotionEvent event) {
//		String msg = "MetroBuzz:Control:";
//		switch (event.getAction() & MotionEvent.ACTION_MASK) {
//		case MotionEvent.ACTION_DOWN:
//			start.x = event.getX();
//			start.y = event.getY();
//			mode = Protocal.DRAG;
//			break;
//		case MotionEvent.ACTION_UP:
//		case MotionEvent.ACTION_POINTER_UP:
//			mode = Protocal.FINGERUP;
//			Log.d(TAG, "mode = NONE");
//			client.setMessage(msg + Protocal.FINGERUP);
//			client.run();
//			break;
//		case MotionEvent.ACTION_MOVE:
//			if (mode == Protocal.DRAG) {
//				float xDiff = event.getX() - start.x;
//				float yDiff = event.getY() - start.y;
//				if (FloatMath.sqrt(xDiff * xDiff + yDiff * yDiff) > 10.0f) {
//					Log.d(TAG, "X: " + event.getX() + " Y: " + event.getY());
//					if (cylinder) {
//						circle.x = event.getX();
//						circle.y = event.getY();
//						client.setMessage(msg + Protocal.DRAG + ":"
//								+ Protocal.cOperation + ":" + circle.info());
//					} else {
//						float xDrag = event.getX() - line.midX();
//						float yDrag = event.getY() - line.midY();
//						line.drag(xDrag, yDrag);
//						client.setMessage(msg + Protocal.DRAG + ":"
//								+ Protocal.vpOperation + ":" + line.info());
//					}
//				}
//			} else if (mode == Protocal.TWOFINGURES) {
//				float newDist = spacing(event);
//				Log.d(TAG, "newDist=" + newDist);
//				if (newDist > 10f) {
//					if (Math.abs(newDist - oldDist) > 10.0f) { // a zoom event
//						if (newDist > oldDist) {
//							if (cylinder) {
//								circle.extend();
//								client.setMessage(msg + Protocal.ZOOMOUT + ":"
//										+ Protocal.cOperation + ":"
//										+ circle.info());
//							} else {
//								line.extend();
//								client.setMessage(msg + Protocal.ZOOMOUT + ":"
//										+ Protocal.vpOperation + ":"
//										+ line.info());
//							}
//						} else {
//							if (cylinder) {
//								circle.compress();
//								client.setMessage(msg + Protocal.ZOOMIN + ":"
//										+ Protocal.cOperation + ":"
//										+ circle.info());
//							} else {
//								line.compress();
//								client.setMessage(msg + Protocal.ZOOMIN + ":"
//										+ Protocal.vpOperation + ":"
//										+ line.info());
//							}
//						}
//					} else if(!cylinder){ // a rotate event
//						//old arc tangent
//						double oldYDiff = Bounds.getBoundY(old.y) - Bounds.getBoundY(pivot.y);
//						double oldXDiff = Bounds.getBoundX(old.x) - Bounds.getBoundX(pivot.x);
//						double oldAngle = Math.atan2(oldYDiff, oldXDiff);
//						
//						//new arc tangent
//						double newYDiff = Bounds.getBoundY(event.getY(1)) - Bounds.getBoundY(pivot.y);
//						double newXDiff = Bounds.getBoundX(event.getX(1)) - Bounds.getBoundX(pivot.x);
//						double newAngle = Math.atan2(newYDiff, newXDiff);
//						
//						line.rotate(oldAngle  - newAngle); 
//						old.set(event.getX(1), event.getY(1));
//						client.setMessage(msg + Protocal.ROTATE + ":"
//								+ Protocal.vpOperation + ":"
//								+ line.info());
//					}
//				}
//				oldDist = newDist;
//			}
//			client.run();
//			circle.invalidate();
//			line.invalidate();
//			break;
//		case MotionEvent.ACTION_POINTER_DOWN:
//			oldDist = spacing(event);
//			pivot.set(event.getX(0), event.getY(0));
//			old.set(event.getX(1), event.getY(1));
//			Log.d(TAG, "oldDist=" + oldDist);
//			if (oldDist > 10.0f) {
//				midPoint(mid, event);
//				mode = Protocal.TWOFINGURES;
//			}
//			break;
//		}
//		return true;
//	}
//
//	private float spacing(MotionEvent event) {
//		float x = event.getX(0) - event.getX(1);
//		float y = event.getY(0) - event.getY(1);
//		return FloatMath.sqrt(x * x + y * y);
//	}
//
//	private void midPoint(PointF point, MotionEvent event) {
//		float x = event.getX(0) + event.getX(1);
//		float y = event.getY(0) + event.getY(1);
//		point.set(x / 2, y / 2);
//	}
//}
